package com.example.bookkeeping_book.service;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.bookkeeping_book.R;
import com.example.bookkeeping_book.domain.AccountBook;
import com.example.bookkeeping_book.utility.MySqlite;
import com.example.bookkeeping_book.utility.Utility;

public class ItemActivity extends AppCompatActivity {

    private ImageView back;
    private TextView consumptionItem;
    private TextView amount;
    private TextView type;
    private TextView date;
    private TextView remarks;
    private TextView consumer;
    private Button del;
    private AccountBook accountBook;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.item_activity);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        init();
    }

    private void init() {
        Intent intent = getIntent();
        int id = intent.getIntExtra("id", -1);
        accountBook = Utility.selectAccountBookById(new MySqlite(this), id);

        back = findViewById(R.id.iv_back2);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        del = findViewById(R.id.btn_del2);

        del.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                androidx.appcompat.app.AlertDialog dialog;
                dialog = new AlertDialog.Builder(ItemActivity.this)
                        .setTitle("提示")
                        .setMessage("是否删除")
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                Utility.deleteAccountBookItem(new MySqlite(ItemActivity.this), id);
                                Toast.makeText(ItemActivity.this, "已删除该记录", Toast.LENGTH_SHORT).show();
                                finish();
                            }
                        })
                        .setNegativeButton("取消", null)
                        .create();
                dialog.show();

            }
        });

        consumptionItem = findViewById(R.id.tv_consumptionItem2);
        amount = findViewById(R.id.tv_amount2);
        type = findViewById(R.id.tv_type);
        date = findViewById(R.id.tv_date2);
        remarks = findViewById(R.id.tv_remarks);
        consumer = findViewById(R.id.tv_consumer2);

        consumptionItem.setText(accountBook.getConsumption_item());
        amount.setText(Double.toString(accountBook.getAmount()));
        type.setText(accountBook.getAmount() > 0 ? "收入" : "支出");
        date.setText(accountBook.getDateToString().trim());
        remarks.setText(accountBook.getRemarks());
        consumer.setText(Utility.selectNameById(new MySqlite(this), accountBook.getConsumerId()));
    }
}